package com.whut.transportation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.whut.transportation.adapter.ReasonAdapter;
import com.whut.transportation.entity.AllReason;
import com.whut.transportation.entity.Reason;
import com.whut.transportation.network.HttpManager;
import com.whut.transportation.network.HttpRequestVo;
import com.whut.transportation.parser.JSONParser;
import com.whut.transportation.ui.BaseActivity;
import com.whut.transportation.ui.BaseActivity.DataCallback;
import com.whut.transportation.utils.Constant;

public class DialogActivity extends BaseActivity {

	private ListView lv_list;
	private List<Reason> datas;
	private LayoutInflater inflater;

	@Override
	protected void loadViewLayout() {
		setContentView(R.layout.dialog);
		inflater = LayoutInflater.from(this);
	}
	
	@Override
	protected void findViewById() {
		lv_list = (ListView) findViewById(R.id.lv_listview);
	}
	
	
	
	private void init_list() {
		
	}

	@Override
	protected void processLogic() {
		datas = new ArrayList<Reason>();
		HttpRequestVo vo = new HttpRequestVo();
		vo.requestUrl = Constant.FIND_REASON_ENTRUCK;
		vo.context = mContext;
		//HashMap<String, String> req = new HashMap<String, String>();
		vo.requestDataMap = null;
		vo.parser = new JSONParser(AllReason.class);
		getDataFromServer(HttpManager.POST_MOTHOD, vo, 
				new DataCallback<HashMap<String, Object>>() {
					@Override
					public void onStart() {
					}
					@Override
					public void onFailed() {
						showNetErrorDialog();
					}
					@Override
					public void processData(
							HashMap<String, Object> paramObject,
							boolean paramBoolean) {
						AllReason allReason = (AllReason) paramObject.get("result");
						datas.addAll(allReason.ListReason);
						
						MyAdapter adapter = new MyAdapter(inflater,datas );
						lv_list.setAdapter(adapter);
						
						lv_list.setOnItemClickListener(new OnItemClickListener() {
							@Override
							public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
								ViewHolder holder = (ViewHolder) view.getTag();
								holder.cb_click.toggle();
								MyAdapter.isSelected.put(position, holder.cb_click.isChecked());
							}
						});
					}
					@Override
					public void onFinish() {
						closeProgressDialog();
					}
				});
	}
	
	public void toBack(View view){
		StringBuffer resultValue = new StringBuffer("[");
		if(view.getId() == R.id.bt_dialog){
			Iterator<Integer> iterator = MyAdapter.isSelected.keySet().iterator();
			while(iterator.hasNext()){
				Integer next = iterator.next();
				Boolean able = MyAdapter.isSelected.get(next);
				if(able){
					resultValue .append(datas.get(next).getDescription()).append(",");
				}
			}
			resultValue.deleteCharAt(resultValue.length()-1);
			resultValue.append("]");
			if(resultValue.length()==1){
				resultValue.deleteCharAt(resultValue.length()-1);
			}
			Intent intent = new Intent();
			intent.putExtra("resultValue", resultValue.toString());
			this.setResult(RESULT_OK, intent);
			finish();
		}
	}
	
	private static class MyAdapter extends BaseAdapter implements OnClickListener{

		public static HashMap<Integer, Boolean> isSelected;
		private LayoutInflater inflater;
		private List<Reason> datas;
		private ViewHolder holder;
		public MyAdapter(LayoutInflater inflater,List<Reason> datas) {
			this.inflater = inflater;
			this.datas = datas;
			init_1();
		}

		private void init_1() {
			if(isSelected == null){
				isSelected = new HashMap<Integer, Boolean>();
				for (int i = 0; i < datas.size(); i++) {
					isSelected.put(i, false);
				}
			}
		}
		@Override
		public int getCount() {
			return datas.size();
		}

		@Override
		public Object getItem(int arg0) {
			return datas.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			if(convertView == null){
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.list_item, null);
				holder.tv_value = (TextView) convertView.findViewById(R.id.tv_item_value);
				holder.cb_click = (CheckBox) convertView.findViewById(R.id.cb_item_select);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			Reason map = datas.get(position);
			String value = (String) map.getDescription();
			
			holder.tv_value.setText(value);
			holder.cb_click.setChecked(isSelected.get(position));
			holder.cb_click.setTag(position);//
			
			holder.cb_click.setOnClickListener(this);
			return convertView;
		}

		@Override
		public void onClick(View v) {
			Integer tag  = (Integer) v.getTag();
			if(isSelected.get(tag)){
				isSelected.put(tag, false);
			}else{
				isSelected.put(tag, true);
			}
			notifyDataSetChanged();
		}
	}
	private static class ViewHolder {
		TextView tv_value;
		CheckBox cb_click;
	}

	@Override
	protected void afterFindViewId() {
	}
	@Override
	protected void setListener() {
	}
	@Override
	protected void getDataAgain() {
		
	}
}
